package edu.dev.tfile.swing.tray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import edu.dev.tfile.TFileEventManagement;
import edu.dev.tfile.common.Utils;

public class TFileTray {

	private MenuItem itemVisit = null;

	private MenuItem itemLoad = null;

	private MenuItem itemStart = null;

	private MenuItem itemStop = null;

	private MenuItem itemDestroy = null;

	private MenuItem itmExit = null;

	public TFileTray() {
		setTray();
	}

	public void setTray() {

		if (SystemTray.isSupported()) {
			SystemTray tray = SystemTray.getSystemTray();
			Image image = Toolkit.getDefaultToolkit().getImage(Utils.BASE_PATH + "/resources/images/tray_icon.png");
			String text = "TFile System";
			PopupMenu popMenu = new PopupMenu();

			itemVisit = new MenuItem("Visit");
			itemVisit.setEnabled(true);
			itemVisit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openBrowse();
				}
			});

			itemLoad = new MenuItem("Load");
			itemLoad.setEnabled(false);
			itemLoad.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TFileEventManagement.getManagement().fire("webserver", "load");
					itemVisit.setEnabled(true);
					itemLoad.setEnabled(false);
					itemStart.setEnabled(false);
					itemStop.setEnabled(true);
					itemDestroy.setEnabled(false);
					itmExit.setEnabled(true);
				}
			});

			itemStart = new MenuItem("Start");
			itemStart.setEnabled(false);
			itemStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TFileEventManagement.getManagement().fire("webserver", "start");
					itemVisit.setEnabled(true);
					itemLoad.setEnabled(false);
					itemStart.setEnabled(false);
					itemStop.setEnabled(true);
					itemDestroy.setEnabled(false);
					itmExit.setEnabled(true);
				}
			});

			itemStop = new MenuItem("Stop");
			itemStop.setEnabled(true);
			itemStop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TFileEventManagement.getManagement().fire("webserver", "stop");
					itemVisit.setEnabled(false);
					itemLoad.setEnabled(false);
					itemStart.setEnabled(true);
					itemStop.setEnabled(false);
					itemDestroy.setEnabled(true);
					itmExit.setEnabled(true);
				}
			});

			itemDestroy = new MenuItem("Destroy");
			itemDestroy.setEnabled(false);
			itemDestroy.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TFileEventManagement.getManagement().fire("webserver", "destroy");
					itemVisit.setEnabled(false);
					itemLoad.setEnabled(true);
					itemStart.setEnabled(false);
					itemStop.setEnabled(false);
					itemDestroy.setEnabled(false);
					itmExit.setEnabled(true);
				}
			});

			itmExit = new MenuItem("Exit");
			itmExit.setEnabled(true);
			itmExit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Exit();
				}
			});

			popMenu.add(itemVisit);
			popMenu.add(itemLoad);
			popMenu.add(itemStart);
			popMenu.add(itemStop);
			popMenu.add(itemDestroy);
			popMenu.add(itmExit);

			TrayIcon trayIcon = new TrayIcon(image, text, popMenu);

			trayIcon.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						openBrowse();
					}
				}
			});

			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				e.printStackTrace();
			}
		}
	}

	private void openBrowse() {
		try {
			String port = Utils.getConfig("server_port");
			Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Utils.getConfig("default_url")
					+ (port.equals("443") ? "" : ":" + port));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void Exit() {
		System.exit(0);
	}

	public static void main(String[] args) {
		new TFileTray();
	}
}
